package net.steam.easyjoboa.web.action;

import java.util.Date;
import java.util.List;

import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.biz.MessageBiz;
import net.steam.easyjoboa.biz.ProclaimBiz;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.Message;
import net.steam.easyjoboa.entity.PageBeans;
import net.steam.easyjoboa.entity.Proclaim;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class MessageAction {
	
	
	private Message message;
	private Message mess;
	private MessageBiz messageBiz;
	private EmployeeBiz employeeBiz;
	private List<Employee> listEmployees;
	List<Message> listOutBox;
	List<Message> noReadMessages;
	List<Message> readMessages;
	private int[] delMesIds;
	private int i =0;
	private int page = 1;
	private PageBeans pageBean;	
	private String tishi;
	private List<Proclaim> proclaims;
	private ProclaimBiz proclaimBiz;
	
	private int mid;
	
	//登陆者
	private Employee loginUser = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
	
	//转到发送消息页面
	@Action(value = "openMessage",results={
		@Result(name="success",location="/SendMessage.jsp")
		})
	public String doOpenMessage(){
		//得到登陆者以外的所有用户
		listEmployees = employeeBiz.getAllEmployeesExceptLoginUserId(loginUser.getUserId());
		if (listEmployees.size()!=0) {
			return "success";
		}
		return "NO";
	}
	
	
	
	//打开回复信息页面
	@Action(value = "restoreMessage",results={
			@Result(name="success",location="/restoreMessage.jsp")
	})
	public String openRestoreMes(){
		message = messageBiz.getMessage(mid);
		if (message!=null) {
			return "success";
		}
		return "input";
	}
	
	
	/**
	 * 发送信件
	 * @return
	 */
	@Action(value="doAddMessage",results={
			@Result(name="success",type="redirect",location="getOutMessageBox"),
			@Result(name="error",type="redirect",location="openMessage"),
			@Result(name="input",location="/SendMessage.jsp")
	})
	public String doAddMessage(){
		Employee getEmployee = employeeBiz.getEmployeeById(mess.getEmployeeByGetId().getUserId());
		Employee user = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		if(getEmployee==null){
			tishi="对不起,你所要发送信息的对象不存在!";
			return "input" ;
		}else if(getEmployee.getUserId().equals(user.getUserId())){
			tishi="不能想自己发送信息";
			return "input";
		}
		Message msg = new Message();
		msg.setContent(mess.getContent());
		msg.setMesTitle(mess.getMesTitle());
		//默认新发送的消息日期和状态
		msg.setSendTime(new Date());
		//默认信件状态为未读
		msg.setMesState("1");
		msg.setSendStatus("0");
		msg.setGetStatus("0");
		msg.setEmployeeByGetId(getEmployee);
		msg.setEmployeeBySendId(user);
		i=messageBiz.addMessage(msg);
		if (i!=0){
			return "success";
		}
		setTishi("发送失败");
		return "error" ;
	}
	
	
	/**
	 * 回复信件
	 * @return
	 */
	@Action(value = "doRestoreMessage",results={
			@Result(name="success",location="/litter.jsp")
	})
	public String doRestoreMessage(){
		//默认回复的消息日期和状态
		mess.setSendTime(new Date());
		//默认信件状态为未读
		mess.setMesState("1");
		mess.setSendStatus("0");
		mess.setGetStatus("0");
		//以登陆者ID作为信息发送者ID		
		mess.setEmployeeBySendId(message.getEmployeeByGetId());
		//以之前发送者ID作为接受者ID
		mess.setEmployeeByGetId(message.getEmployeeBySendId());		
		i = messageBiz.addMessage(mess);		
		return "success";		
	}
	

	/**
	 * 得到本人所发信件
	 * @return
	 */
	@Action(value = "getOutMessageBox",results={
			@Result(name="success",location="/outMessageBox.jsp")
	})
	public String getOutBoxMessage(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(page);
		listOutBox = messageBiz.findOutBox(pageBean, message, loginUser.getUserId());
		return "success";
	}		

	/**
	 * 得到本人所收的未读信件
	 * @return
	 */
	@Action(value = "getNoReadMessageBox",results={
			@Result(name="success",location="/noReadMessageBox.jsp")
	})
	public String getNoReadMessageBox(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(page);
		noReadMessages = messageBiz.findInBoxNoRead(pageBean, message, loginUser.getUserId());
		return "success";
	}
	
	
	/**
	 * 得到本人所收的已读信件
	 * @return
	 */
	@Action(value = "getReadMessageBox",results={
			@Result(name="success",location="/readMessageBox.jsp")
	})
	public String getReadMessageBox(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(page);
		readMessages = messageBiz.findInBoxRead(pageBean, message, loginUser.getUserId());
		return "success";
	}
	
	


	/**
	 * 阅读并预备回复信件
	 */
	@Action(value = "doReadMessage",results={
			@Result(name="success",location="/readingMessage.jsp")
	})
	public String doReadingMessage(){
		message = messageBiz.getMessage(mid);
		messageBiz.updateMessage(message);
		return "success";
	}
	
	/**
	 * 阅读信件
	 */
	@Action(value = "detailMessage",results={
			@Result(name="success",location="/detailMessage.jsp")
	})
	public String detailMessage(){
		message = messageBiz.getMessage(mid);
		return "success";
	}
	
	/**
	 * 逻辑删除指定接收的已读信件
	 * @return
	 */
	@Action(value = "doDelMessageGet",results={
			@Result(name="success",type="redirect",location="getReadMessageBox")
	})
	public String doDelMessageGet(){
		if (delMesIds.length!=0) {
			messageBiz.delMessageGet(delMesIds);
			return "success";
		}
		return "no";
	}
	
	/**
	 * 逻辑删除指定接收的未读信件
	 * @return
	 */
	@Action(value = "doDelMessage",results={
			@Result(name="success",type="redirect",location="getNoReadMessageBox")
	})
	public String doDelMessage(){
		if (delMesIds.length!=0) {
			messageBiz.delMessageGet(delMesIds);
			return "success";
		}
		return "no";
	}
	
	/**
	 * 逻辑删除所选中的所发信件
	 * @return
	 */
	@Action(value = "doDelMessageSend",results={
			@Result(name="success",type="redirect",location="getOutMessageBox")
	})
	public String doDelMessageSend(){		
		if (delMesIds.length!=0) {
			messageBiz.delMessageSend(delMesIds);
			return "success";
		}
		return "no";
	}
	

	public void setMessageBiz(MessageBiz messageBiz) {
		this.messageBiz = messageBiz;
	}	

	public List<Message> getListOutBox() {
		return listOutBox;
	}

	public void setListOutBox(List<Message> listOutBox) {
		this.listOutBox = listOutBox;
	}

	public int getPage() {
		return page;
	}	
	
	public void setPage(int page) {
		this.page = page;
	}


	public List<Employee> getListEmployees() {
		return listEmployees;
	}

	public void setListEmployees(List<Employee> listEmployees) {
		this.listEmployees = listEmployees;
	}
	
	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}
	
	public Employee getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Employee loginUser) {
		this.loginUser = loginUser;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public int getMid() {
		return mid;
	}

	public void setMid(int mid) {
		this.mid = mid;
	}

	public List<Message> getNoReadMessages() {
		return noReadMessages;
	}

	public void setNoReadMessages(List<Message> noReadMessages) {
		this.noReadMessages = noReadMessages;
	}

	public List<Message> getReadMessages() {
		return readMessages;
	}

	public void setReadMessages(List<Message> readMessages) {
		this.readMessages = readMessages;
	}

	public int[] getDelMesIds() {
		return delMesIds;
	}

	public void setDelMesIds(int[] delMesIds) {
		this.delMesIds = delMesIds;
	}

	public Message getMess() {
		return mess;
	}

	public void setMess(Message mess) {
		this.mess = mess;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}



	public PageBeans getPageBean() {
		return pageBean;
	}



	public void setPageBean(PageBeans pageBean) {
		this.pageBean = pageBean;
	}

	public String getTishi() {
		return tishi;
	}

	public void setTishi(String tishi) {
		this.tishi = tishi;
	}


	public ProclaimBiz getProclaimBiz() {
		return proclaimBiz;
	}

	public void setProclaimBiz(ProclaimBiz proclaimBiz) {
		this.proclaimBiz = proclaimBiz;
	}



	public List<Proclaim> getProclaims() {
		return proclaims;
	}



	public void setProclaims(List<Proclaim> proclaims) {
		this.proclaims = proclaims;
	}

	
}
